package toubiao.service.impl.init;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.sound.sampled.Port;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import toubiao.dao.support.JspPageDaoI;
import toubiao.dao.support.ResourceDaoI;
import toubiao.dao.support.RoleDaoI;
import toubiao.domain.po.manage.PoJspPage;
import toubiao.domain.po.manage.PoResource;
import toubiao.domain.po.manage.PoRole;
import toubiao.service.init.InitResourceServiceI;

@Service
public class InitResourceServiceImpl implements InitResourceServiceI {

	@Autowired
	private ResourceDaoI resourceDao;
	
	
	@Autowired
	private JspPageDaoI jspPageDao;
	
	
	@Override
	public void clearAndInit(){
		clearResource();
		init();
	}
	
	
	@Override
	public void init(){
		initResourceOnLoginJsp();
		initResourceOnWestJspSub();
		initResourceOnIndexJsp();
	}
	
	@Override
	public void clearResource(){
		String hql="from PoResource";
		List<PoResource> resourceList=resourceDao.find(hql);
		for(PoResource poResource:resourceList){
			resourceDao.delete(poResource);
		}
	}
	
	@Override
	public void initResourceOnLoginJsp(){
		PoJspPage loginJsp=new PoJspPage();
		loginJsp.setName("login.jsp");
		loginJsp.setJspPath("/module/login/login");
		loginJsp.setType("jsp");
		Set<PoResource> jspResourceSet=new HashSet<>();
		loginJsp.setResourceSet(jspResourceSet);
		
		PoResource jspLogin=new PoResource();
		jspLogin.setType("jsp");
		jspLogin.setAucType("public");
		jspLogin.setUrl("/login/login.jspPage");
		
		PoResource jspIndex =new PoResource();
		jspIndex.setType("jsp");
		jspIndex.setUrl("/login/index.jspPage");
		
		PoResource jsonLoginResult =new PoResource();
		jsonLoginResult.setType("json");
		jsonLoginResult.setUrl("/login/loginResult.json");
		jsonLoginResult.setAucType("public");
		
		PoResource fileDownloadFireFox =new PoResource();
		fileDownloadFireFox.setType("file");
		fileDownloadFireFox.setUrl("/download/public");
		fileDownloadFireFox.setAucType("public");
		
		jspResourceSet.add(jspLogin);
		jspResourceSet.add(jspIndex);
		jspResourceSet.add(jsonLoginResult);
		jspResourceSet.add(fileDownloadFireFox);
		
		jspPageDao.save(loginJsp);
		
	}
	
	
	public void initResourceOnIndexJsp(){
		PoJspPage indexJsp=new PoJspPage();
		indexJsp.setName("index.jsp");
		indexJsp.setJspPath("/module/layout/index");
		indexJsp.setType("jsp");
		Set<PoResource> jspResourceSet=new HashSet<>();
		indexJsp.setResourceSet(jspResourceSet);
		
		PoResource subJspWest =new PoResource();
		subJspWest.setType("subJsp");
		subJspWest.setUrl("/layout/west.subJsp");
		
		PoResource jspInfom =new PoResource();
		jspInfom.setType("jsp");
		jspInfom.setUrl("/infom/infom.jspPage");
		
		PoResource subJspSouth =new PoResource();
		subJspSouth.setType("subJsp");
		subJspSouth.setUrl("/layout/south.jspPage");
		
		jspResourceSet.add(subJspSouth);
		jspResourceSet.add(subJspWest);
		jspResourceSet.add(jspInfom);
		
		jspPageDao.save(indexJsp);
		
	}
	
	    /**
	    * @Title: initResourceOnWestJspSub
	    * @Description: 初始化 index.jsp 左边的westJspSub页面上的资源
	    * @param     
	    * @return void    
	    * @throws
	    */
	@Override
	public void initResourceOnWestJspSub(){
		PoJspPage jspSub=new PoJspPage();
		jspSub.setName("west.jspSub");
		jspSub.setJspPath("/module/layout/west");
		jspSub.setType("jspSub");
		Set<PoResource> jspResourceSet=new HashSet<>();
		jspSub.setResourceSet(jspResourceSet);
	/*	
	 * 	引入该jsp的servlet resource 在引入该jsp的页面体现
		PoResource jspServlet=new PoResource();
		jspServlet.setName("west.jspSub");
		jspServlet.setType("jsp");
		jspServlet.setUrl(null);*/
		
		PoResource accordionPanelOfManage =new PoResource();
		accordionPanelOfManage.setName("管理模块");
		accordionPanelOfManage.setType("resourceContainer");
		accordionPanelOfManage.setUrl("/layout/west/accordionPanel.json");
		accordionPanelOfManage.setChildrenSet(new HashSet<PoResource>());
		
		
		PoResource menuItemOfUser =new PoResource();
		menuItemOfUser.setName("用户管理");
		menuItemOfUser.setUrl("null");
		menuItemOfUser.setType("jsp");
		menuItemOfUser.setIcon("database_gear");
		
		PoResource menuItemOfRole =new PoResource();
		menuItemOfRole.setName("角色管理");
		menuItemOfRole.setUrl("null");
		menuItemOfRole.setType("jsp");
		menuItemOfRole.setIcon("database_gear");
		
		accordionPanelOfManage.getChildrenSet().add(menuItemOfUser);
		accordionPanelOfManage.getChildrenSet().add(menuItemOfRole);
		
		
		PoResource accordionPanelOfService =new PoResource();
		accordionPanelOfService.setName("业务模块");
		accordionPanelOfService.setType("resourceContainer");
		accordionPanelOfService.setUrl("/layout/west/accordionPanel.json");
		accordionPanelOfService.setChildrenSet(new HashSet<PoResource>());
		
		PoResource menuItemOfProject =new PoResource();
		menuItemOfProject.setName("项目信息");
		menuItemOfProject.setUrl("/project/grid.jspPage");
		menuItemOfProject.setType("jsp");
		menuItemOfProject.setIcon("database_gear");
		
		PoResource menuItemOfContract =new PoResource();
		menuItemOfContract.setName("合同信息");
		menuItemOfContract.setUrl("/contract/grid.jspPage");
		menuItemOfContract.setType("jsp");
		menuItemOfContract.setIcon("database_gear");
		
		PoResource menuItemOfPerson =new PoResource();
		menuItemOfPerson.setName("人员信息");
		menuItemOfPerson.setUrl("null");
		menuItemOfPerson.setType("jsp");
		menuItemOfPerson.setIcon("database_gear");
		
		PoResource menuItemOfReward =new PoResource();
		menuItemOfReward.setName("获奖信息");
		menuItemOfReward.setUrl("null");
		menuItemOfReward.setType("jsp");
		menuItemOfReward.setIcon("database_gear");
		
		PoResource menuItemOfTest =new PoResource();
		menuItemOfTest.setName("test");
		menuItemOfTest.setUrl("/common/dataGrid.jspPage");
		menuItemOfTest.setType("jsp");
		menuItemOfTest.setIcon("database_gear");
		
		accordionPanelOfService.getChildrenSet().add(menuItemOfProject);
		accordionPanelOfService.getChildrenSet().add(menuItemOfContract);
		accordionPanelOfService.getChildrenSet().add(menuItemOfPerson);
		accordionPanelOfService.getChildrenSet().add(menuItemOfReward);
		accordionPanelOfService.getChildrenSet().add(menuItemOfTest);
		
		jspResourceSet.addAll(accordionPanelOfManage.getChildrenSet());
		jspResourceSet.add(accordionPanelOfManage);
		jspResourceSet.addAll(accordionPanelOfService.getChildrenSet());
		jspResourceSet.add(accordionPanelOfService);
		
		
		jspPageDao.save(jspSub);
	}
	
	
	    
	
	
}
